<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2017/4/27
 * Time: 10:51
 */

namespace app\models;

use app\librarys\DbCacheUtility;
use yii\caching\DbCache;


/**
 * 后台管理员
 * Class Manager
 * @package app\models
 */
class Manager
{
    const SessionKey = 'session_key_manager';

    /**
     * 获取当前登录用户(如果为null,则未登录)
     * @return mixed|null
     */
    public static function getCurrentManager()
    {
        if (\Yii::$app->session->has(self::SessionKey)) {
            $manager = \Yii::$app->session->get(self::SessionKey);
//            $manager['is_department_admin'] = Department::isDepartmentManager($manager['id']);
//            $department = Department::getDepartmentByManagerId($manager['id']);
//            $manager['department'] = $department;


            return $manager;
        }
        if (YII_DEBUG) {
            $item = self::getManagerById('0'); //调试默认使用0 admin账号
            return $item;
        }

//        if (YII_ENV == 'dev') {
//            //调试用---begin
//            $currentManager = self::validatePassword('admin', '1');
//            self::setCurrentManager($currentManager);
//            return $currentManager;
//            //-----end
//        }

        return null;
    }

    /**
     * 用户登录以后,设置到session中
     * @param $manager
     */
    public static function setCurrentManager($manager)
    {
        \Yii::$app->session->remove(self::SessionKey);
        \Yii::$app->session->set(self::SessionKey, $manager);
    }

    /**
     * 用户登录验证
     * @param $account
     * @param string $password md5加密
     * @return array|false|null
     */
    public static function validatePassword($account, $password)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT id,name,account,phone,is_enabled,remark,is_deleted,create_datetime,modify_datetime FROM t_manager WHERE account = :account AND password = lower(md5(concat(:password,password_token))) AND is_enabled = 1 AND is_deleted = 0');
        $cmd->bindValue(':account', $account);
        $cmd->bindValue(':password', $password);
        $item = DbCacheUtility::queryOne($cmd, DbCacheUtility::NoCacheTime);
        return $item;
    }

    /**
     * 退出登录
     */
    public static function logout()
    {
        \Yii::$app->session->removeAll();
    }

    /**
     * 获取系统所有管理员总数(主要用于分页)
     * @return false|null|string
     */
    public static function getCount()
    {
        $cmd = \Yii::$app->db->createCommand('SELECT COUNT(1) FROM t_manager');
        $count = DbCacheUtility::queryScalar($cmd);
        return $count;
    }

    /**
     * 通过id获取mananger
     * @param $id
     * @return mixed
     */
    public static function getManagerById($id)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_manager WHERE id = :id');
        $cmd->bindValue(':id', $id);
        $item = DbCacheUtility::queryOne($cmd);
        return $item;
    }

    /**
     * 获取管理员列表
     * @param int $offset 分页查询起始位置
     * @param int $limit 分页查询每页数量
     * @return array
     */
    public static function getList($offset, $limit)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_manager ORDER BY create_datetime DESC LIMIT :offset, :limit');
        $cmd->bindValue(':offset', $offset);
        $cmd->bindValue(':limit', $limit);

        $items = DbCacheUtility::queryAll($cmd);

        return $items;
    }

    public static function getAppVersion($app_code,$app_type)
    {
        $cmd = \Yii::$app->db->createCommand('select * from t_app_version where app_code=:app_code and app_type=:app_type');
        $cmd->bindValue(':app_code', $app_code);
        $cmd->bindValue(':app_type', $app_type);

        $items = DbCacheUtility::queryOne($cmd);

        return $items;
    }
}